var dataLoader = null;

function DataLoader(tableId, league, season, klasse, processingUrl){
	this.table = document.getElementById(tableId);
	this.league = league;
	this.season = season;
	this.klasse = klasse;
	this.processingUrl = processingUrl;
	this.dataLoader = this;
	dataLoader = this;
}

DataLoader.prototype = {
	
	setErrMsg : function(msg){
		var tr = this.table.insertRow(0);
  		var td = document.createElement("td");
  		var td_text = document.createTextNode(msg);
  		td.appendChild(td_text);
  		td.className="text";
  		tr.appendChild(td);
	},
	
	validate : function(response){
		var erg = response.getElementsByTagName('ergebnis')[0].childNodes[0].nodeValue;
		if (erg == 'error'){
			var msg = response.getElementsByTagName('nachricht')[0].childNodes[0].nodeValue;
			dataLoader.setErrMsg(msg);
		} else {
			setData(dataLoader.table, response);
		}
	},
	send : function(e){
		
		//request bauen
		var liga = "liga=" + dataLoader.league;
		var saison = "saison=" + dataLoader.season;
		var team = "klasse=" + dataLoader.klasse;
		var request = '?' + liga + '&' + saison + '&' + team;
		httpRequest.abort();  

		httpRequest.onreadystatechange = function() {
			if ( httpRequest.readyState == 4 ) {
			    response = httpRequest.responseXML;
			    if (response != null){
			    	dataLoader.validate(response);
			    }
				document.getElementById("loading").style.display = 'none';
				document.getElementById("loading2").style.display = 'none';
			}
		};

	    httpRequest.open('GET', dataLoader.processingUrl + request, true);
		document.getElementById("loading").style.display = 'block';
		document.getElementById("loading2").style.display = 'block';
	    httpRequest.send(null);
	}

}